import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import echarts from "echarts";
import "echarts/map/js/china.js";
import axios from "axios";
import qs from "qs";
import VueQuillEditor from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import "./assets/response.css";
import {
  Menu,
  Icon,
  Input,
  BackTop,
  Layout,
  Row,
  Col,
  Card,
  Carousel,
  Space,
  Spin,
  Tabs,
  Form,
  Button,
  Checkbox,
  AutoComplete,
  Select,
  Cascader,
  Tooltip,
  message,
  Popconfirm,
  Statistic,
  Skeleton,
  List,
  Comment,
  Breadcrumb,
  Progress,
  Avatar,
  Table,
  Tag,
  Modal,
  Switch,
  Radio,
  Timeline,
  Descriptions,
  Badge,
  Empty,
  Upload,
  Drawer,
  FormModel,
} from "ant-design-vue";
import "ant-design-vue/dist/antd.css";

Vue.config.productionTip = false;

// 全局图片的地址
// Vue.prototype.imgUrl = "http://localhost/api/";
Vue.prototype.imgUrl = "http://demoblog.bay666.top/api/";

// 图表
Vue.prototype.$echarts = echarts;

// axios
Vue.prototype.$axios = axios.create({
  transformRequest: [
    function (data) {
      return qs.stringify({
        ...data,
      });
    },
  ],
});
// axios拦截器
Vue.prototype.$axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  const { token } = JSON.parse(localStorage.getItem("userData")) || false;
  config.headers.TOKEN = token;
  return config;
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const info = JSON.parse(localStorage.getItem("userData")) || false;
  if (to.matched[0].path === "/admin") {
    // 已登录 去后台
    if (info && info.token && info.type === "1") {
      next();
    } else {
      // 未登录 跳转登录
      next("/client/login");
    }
  }

  next();
});
// 返回到顶部
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0);
});

// 全局提示框
Vue.prototype.$message = message;

// 配置全局过滤器 转换数字为 123,233的格式
Vue.filter("toThousands", (num) => {
  return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
});

Vue.use(Menu)
  .use(Icon)
  .use(BackTop)
  .use(Input)
  .use(Layout)
  .use(Row)
  .use(Col)
  .use(Card)
  .use(Space)
  .use(Spin)
  .use(Carousel)
  .use(Form)
  .use(Button)
  .use(Checkbox)
  .use(Tabs)
  .use(Cascader)
  .use(Tooltip)
  .use(Select)
  .use(AutoComplete)
  .use(message)
  .use(Popconfirm)
  .use(Statistic)
  .use(Comment)
  .use(List)
  .use(Breadcrumb)
  .use(Skeleton)
  .use(Avatar)
  .use(Progress)
  .use(Tag)
  .use(Modal)
  .use(Switch)
  .use(Radio)
  .use(Timeline)
  .use(Descriptions)
  .use(Badge)
  .use(Upload)
  .use(Drawer)
  .use(Empty)
  .use(FormModel)
  .use(VueQuillEditor)
  .use(Table);

new Vue({
  router,
  render: (h) => h(App),
  // 使用全局事件总线 设置用户登录的信息
  beforeCreate() {
    this.__proto__.$bus = this;
  },
}).$mount("#app");
